import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

let updateStorage = function () {
    if (window.localStorage) {
        window.localStorage.setItem('todos', JSON.stringify(store.state.todos));
    }
}

let store = new Vuex.Store({
    state: {
        todos: [],
        messages: []
    },
    mutations: {
        initTodo: function (state) {
            if (!window.localStorage) return;
            let todos = window.localStorage.getItem('todos');
            if (todos) {
                state.todos = JSON.parse(todos);
            }
        },
        addTodo: function (state, todoContent) {
            state.todos.push({
                content: todoContent,
                active: true
            });
            updateStorage();
        },
        removeTodo: function (state, index) {
            state.todos.splice(index, 1);
            updateStorage();
        },
        editTodo: function (state, {index, todo}) {
            if (todo.active !== undefined)
                state.todos[index].active = todo.active;
            if (todo.content !== undefined)
                state.todos[index].content = todo.content;
            updateStorage();
        },
        allTodo: function (state, active) {
            for (let todo of state.todos) {
                todo.active = active;
            }
            updateStorage();
        },
        addMessage: function (state, {msg, delay}) {
            let time = new Date().getTime();
            let store = this;
            if (delay == null || delay == undefined) delay = 5;
            if (delay > 0) {
                setTimeout(function() {
                    let messages = state.messages;
                    for (let msg of messages) {
                        if (msg.time !== time) continue;
                        let n = messages.indexOf(msg);
                        if (n > -1) store.dispatch('removeMessage', n);
                    }
                }, delay * 1000);
            }
            state.messages.push({
                msg,
                time
            });
        },
        removeMessage: function (state, index) {
            state.messages.splice(index, 1);
        },
        removeAllMessage: function (state) {
            state.messages.splice(0, state.messages.length);
        }
    },
    actions: {
        initTodo: function ({commit}) {
            commit('initTodo');
        },
        addTodo: function ({commit}, todoContent) {
            commit('addTodo', todoContent);
        },
        removeTodo: function ({commit}, index) {
            commit('removeTodo', index);
        },
        editTodo: function ({commit}, {index, todo}) {
            commit('editTodo', {index, todo});
        },
        allTodo: function ({commit}, active) {
            commit('allTodo', active);
        },
        addMessage: function ({commit}, {msg, delay}) {
            commit('addMessage', {msg, delay});
        },
        removeMessage: function ({commit}, index) {
            commit('removeMessage', index);
        },
        removeAllMessage: function ({commit}) {
            commit('removeAllMessage');
        }
    }
});

export default store;
